﻿using System;
using System.Collections.Generic;
using Dapper;
using zhangmenren.Db;
using zhangmenren.Models;

namespace zhangmenren.Repository
{
    public class CommentRepository:BaseRepository
    {
        public IEnumerable<CommentEntity> FindByWorkId(int workid)
        {
            string sql = @"SELECT Id,HeadImg,UserName,[Content],[HuiFu],AddDT FROM Comment WHERE WorkId=@WorkId ORDER BY Id DESC";
            return Execute(connection => connection.Query<CommentEntity>(sql,new { WorkId =workid}));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
        }
        public IEnumerable<CommentEntity> FindByZMId(int zmid)
        {
            string sql = @"SELECT Id,HeadImg,UserName,[Content],[HuiFu],AddDT FROM Comment WHERE ZMID=@ZMID ORDER BY Id DESC";
            return Execute(connection => connection.Query<CommentEntity>(sql, new { ZMID = zmid }));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
        }
        public IEnumerable<CommentEntity> Select()
		{
			string sql = @"SELECT Id,HeadImg,UserName,[Content],[HuiFu],AddDT FROM Comment ORDER BY Id DESC";
			return Execute(connection => connection.Query<CommentEntity>(sql));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
		}
		
		public int Del(int id)
		{
			string sql = @"DELETE FROM Comment WHERE Id = @Id";
			return Execute(connection => connection.Execute(sql,new {Id=id }));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
		}

        public int Up(int id,string huifu)
        {
            string sql = @"UPDATE Comment SET HuiFu = @HuiFu WHERE Id = @Id ";
            return Execute(connection => connection.Execute(sql, new { Id = id,HuiFu=huifu  }));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
        }

        public int Add(CommentEntity entity)
        {
            string sql = @"INSERT INTO Comment 
                            (HeadImg,ZMID
                            ,UserName
                            ,Content
                            ,WorkId
                            ,AddDT)
                        VALUES
                            (@HeadImg,@ZMID
                            ,@UserName
                            ,@Content
                            ,@WorkId
                            ,GETDATE())";
            return Execute(connection => connection.Execute(sql, entity));
        }
    }
}
